package com.acxy.ants.common.kit;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

public class PathKit {

    public static String getClassPath() {
        try {
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
            return URLDecoder.decode(path, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    //带file:前缀
    public static URL getClassURL() {
        return PathKit.class.getClassLoader().getResource("");
    }


    /**
     * 获取jar所在位置
     * @return
     */
    public static String getJarPath() {
        URL url = PathKit.class.getProtectionDomain().getCodeSource().getLocation();
        String filePath = null;
        try {
            filePath = URLDecoder.decode(url.getPath(), "utf-8");// 转化为utf-8编码
        } catch (Exception e) {
            e.printStackTrace();
        }

        return filePath;
    }

    public static String ProjectPath() {
        try {
            return new File("").getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getClassPath(Class cls){
        try {
            String path = cls.getResource("/").getPath();
            return URLDecoder.decode(path, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

}


